package com.cskaoyan.controller.system;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketStorage;
import com.cskaoyan.service.system.ObjectStorageService;
import com.cskaoyan.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

/**
 * 对象存储模块
 */
@RestController
@RequestMapping("admin/storage")
public class ObjectStorage {


    @Value("${file.imgPath}")
    private String imgPath;

    @Value("${admin.localhost}")
    private String localhost;

    @Autowired
    ObjectStorageService objectStorageService;


    @RequestMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody MarketStorage marketStorage) {
        MarketStorage storage = objectStorageService.deleteStorage(marketStorage);
        String url = storage.getUrl();
        int index = url.lastIndexOf("/");
        String imgName = url.substring(index);
        File file = new File(imgPath + imgName);
        if (file.exists()) {
            file.delete();
        }
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }


    @RequestMapping("update")
    public BaseRespVo<MarketStorage> update(@RequestBody MarketStorage marketStorage) {
        //响应：{"errno":0,"data":{"id":215,"key":"k8frpsuddwrd7zfmi1hg.jpg","name":"shun-jellyfish05.jpg","type":"image/jpeg","size":391254,"url":"http://182.92.235.201:8083/wx/storage/fetch/k8frpsuddwrd7zfmi1hg.jpg","addTime":"2022-11-21 10:51:24","updateTime":"2022-11-21 11:04:01","deleted":false},"errmsg":"成功"}
        String url = marketStorage.getUrl();
        String replaceUrl = url.replace(Constant.DOMAIN, "");
        marketStorage.setUrl(replaceUrl);
        MarketStorage marketStorage1 = objectStorageService.updateStorage(marketStorage);

        BaseRespVo<MarketStorage> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(marketStorage1);
        return baseRespVo;
    }


    /**
     * 对象存储显示和查询
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param request
     * @return 2022年11月21日11:04:49
     */
    @RequestMapping("list")
    public BaseRespVo<CommonData<MarketStorage>> list(@RequestParam("page") Integer page,
                                                      @RequestParam("limit") Integer limit,
                                                      @RequestParam("sort") String sort,
                                                      @RequestParam("order") String order,
                                                      HttpServletRequest request) {
        //{"errno":0,"data":{"total":59,"pages":3,"limit":20,"page":1,"list":[{"id":213,"key":"ny2b76wy9ai4m5a9y6xc.jpg","name":"wallhaven-4195.jpg","type":"image/jpeg","size":599751,"url":"http://182.92.235.201:8083/wx/storage/fetch/ny2b76wy9ai4m5a9y6xc.jpg","addTime":"2022-11-21 10:50:29","updateTime":"2022-11-21 10:50:29","deleted":false},{"id":195,"key":"yhwyiosrc58tnxsl0ei0.jpg","name":"22496_1240955_433399.jpg","type":"image/jpeg","size":192594,"url":"http://182.92.235.201:8083/wx/storage/fetch/yhwyiosrc58tnxsl0ei0.jpg","addTime":"2022-11-20 23:18:42","updateTime":"2022-11-20 23:18:42","deleted":false},{"id":194,"key":"wwrv24f526snzq27zaqw.jpg","name":"22496_1240947_963457.jpg","type":"image/jpeg","size":58965,"url":"http://182.92.235.201:8083/wx/storage/fetch/wwrv24f526snzq27zaqw.jpg","addTime":"2022-11-20 23:07:05","updateTime":"2022-11-20 23:07:05","deleted":false},{"id":193,"key":"6v56n45ladt0v05xkrj6.jpg","name":"22496_1240945_386490.jpg","type":"image/jpeg","size":60598,"url":"http://182.92.235.201:8083/wx/storage/fetch/6v56n45ladt0v05xkrj6.jpg","addTime":"2022-11-20 23:07:03","updateTime":"2022-11-20 23:07:03","deleted":false},{"id":168,"key":"d47bvwi5sxyz0eki13ea.jpg","name":"157123055832115669931721908195203.jpg","type":"image/jpeg","size":229478,"url":"http://182.92.235.201:8083/wx/storage/fetch/d47bvwi5sxyz0eki13ea.jpg","addTime":"2022-11-20 21:42:12","updateTime":"2022-11-20 21:42:12","deleted":false},{"id":167,"key":"lct567tot4wa3nwcvzyf.jpg","name":"15712302279071567429968611lo.jpg","type":"image/jpeg","size":58767,"url":"http://182.92.235.201:8083/wx/storage/fetch/lct567tot4wa3nwcvzyf.jpg","addTime":"2022-11-20 21:42:10","updateTime":"2022-11-20 21:42:10","deleted":false},{"id":166,"key":"y30f1dyp9dupo396lho3.png","name":"615a16e899e01efb780c488df4233f48.png","type":"image/png","size":226265,"url":"http://182.92.235.201:8083/wx/storage/fetch/y30f1dyp9dupo396lho3.png","addTime":"2022-11-20 21:41:26","updateTime":"2022-11-20 21:41:26","deleted":false},{"id":164,"key":"q2jfoqg9zgtx8w6i7xhl.jpg","name":"22496_1240955_433399.jpg","type":"image/jpeg","size":192594,"url":"http://182.92.235.201:8083/wx/storage/fetch/q2jfoqg9zgtx8w6i7xhl.jpg","addTime":"2022-11-20 21:40:30","updateTime":"2022-11-20 21:40:30","deleted":false},{"id":163,"key":"7iu95so66d2takvsvp2h.png","name":"615a16e899e01efb780c488df4233f48.png","type":"image/png","size":226265,"url":"http://182.92.235.201:8083/wx/storage/fetch/7iu95so66d2takvsvp2h.png","addTime":"2022-11-20 21:40:18","updateTime":"2022-11-20 21:40:18","deleted":false},{"id":162,"key":"7v8z5oxn9s8e5iftalx0.jpg","name":"22496_1240950_521221.jpg","type":"image/jpeg","size":110605,"url":"http://182.92.235.201:8083/wx/storage/fetch/7v8z5oxn9s8e5iftalx0.jpg","addTime":"2022-11-20 21:39:47","updateTime":"2022-11-20 21:39:47","deleted":false},{"id":161,"key":"wdvoqy8vhu89qz8s2xb0.png","name":"22496_1240962_598604.png","type":"image/png","size":196327,"url":"http://182.92.235.201:8083/wx/storage/fetch/wdvoqy8vhu89qz8s2xb0.png","addTime":"2022-11-20 21:39:45","updateTime":"2022-11-20 21:39:45","deleted":false},{"id":155,"key":"b8jfn7omb31rjcvv6m58.jpg","name":"157123055832115669931721908195203.jpg","type":"image/jpeg","size":229478,"url":"http://182.92.235.201:8083/wx/storage/fetch/b8jfn7omb31rjcvv6m58.jpg","addTime":"2022-11-20 21:02:57","updateTime":"2022-11-20 21:02:57","deleted":false},{"id":154,"key":"h7ugb2656x378nqyy9dj.jpg","name":"15712302279071567429968611lo.jpg","type":"image/jpeg","size":58767,"url":"http://182.92.235.201:8083/wx/storage/fetch/h7ugb2656x378nqyy9dj.jpg","addTime":"2022-11-20 21:02:53","updateTime":"2022-11-20 21:02:53","deleted":false},{"id":149,"key":"vq1mjizmz9jqcvrt35dq.jpeg","name":"0ccfeb51bf4e33ca3e84ffd9e7e5bd51.jpeg","type":"image/jpeg","size":75774,"url":"http://182.92.235.201:8083/wx/storage/fetch/vq1mjizmz9jqcvrt35dq.jpeg","addTime":"2022-11-20 20:38:27","updateTime":"2022-11-20 20:38:27","deleted":false},{"id":148,"key":"0io77vdnt5ecmeh4j7w2.jpeg","name":"0ccfeb51bf4e33ca3e84ffd9e7e5bd51.jpeg","type":"image/jpeg","size":75774,"url":"http://182.92.235.201:8083/wx/storage/fetch/0io77vdnt5ecmeh4j7w2.jpeg","addTime":"2022-11-20 20:37:40","updateTime":"2022-11-20 20:37:40","deleted":false},{"id":147,"key":"4r7ebujdexq7i22uwvzo.jpeg","name":"0ccfeb51bf4e33ca3e84ffd9e7e5bd51.jpeg","type":"image/jpeg","size":75774,"url":"http://182.92.235.201:8083/wx/storage/fetch/4r7ebujdexq7i22uwvzo.jpeg","addTime":"2022-11-20 20:37:37","updateTime":"2022-11-20 20:37:37","deleted":false},{"id":139,"key":"789anxlor03s5blu46rx.jpg","name":"f098529fde491806415a740303a045ea.jpg","type":"image/jpeg","size":50632,"url":"http://182.92.235.201:8083/wx/storage/fetch/789anxlor03s5blu46rx.jpg","addTime":"2022-11-20 20:14:24","updateTime":"2022-11-20 20:14:24","deleted":false},{"id":137,"key":"f0hmva0kscjwykkenvlz.jpg","name":"22496_1240947_963457.jpg","type":"image/jpeg","size":58965,"url":"http://182.92.235.201:8083/wx/storage/fetch/f0hmva0kscjwykkenvlz.jpg","addTime":"2022-11-20 20:06:26","updateTime":"2022-11-20 20:06:26","deleted":false},{"id":136,"key":"yhryc0b6garkc6sknikw.jpg","name":"22496_1240950_521221.jpg","type":"image/jpeg","size":110605,"url":"http://182.92.235.201:8083/wx/storage/fetch/yhryc0b6garkc6sknikw.jpg","addTime":"2022-11-20 20:06:03","updateTime":"2022-11-20 20:06:03","deleted":false},{"id":135,"key":"bw1ydrzbh7iysjzeudmc.jpg","name":"22496_1240955_433399.jpg","type":"image/jpeg","size":192594,"url":"http://182.92.235.201:8083/wx/storage/fetch/bw1ydrzbh7iysjzeudmc.jpg","addTime":"2022-11-20 20:05:49","updateTime":"2022-11-20 20:05:49","deleted":false}]},"errmsg":"成功"}
        String name = request.getParameter("name");
        String key = request.getParameter("key");
        CommonData<MarketStorage> marketAdminCommonData = objectStorageService.selectStorage(page, limit, sort, order, name, key);
        for (MarketStorage marketStorage : marketAdminCommonData.getList()) {
            marketStorage.setUrl(Constant.DOMAIN + marketStorage.getUrl());
        }
        BaseRespVo<CommonData<MarketStorage>> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(marketAdminCommonData);
        return baseRespVo;
    }


    /**
     * 角色管理
     * 增加照片
     *
     * @param file
     * @throws IOException 2022年11月20日14:40:54
     */
    @RequestMapping("create")
    public BaseRespVo<MarketStorage> create(MultipartFile file) throws IOException {
        //响应：{"errno":0,"data":{"id":79,"key":"kutxipgk4u754ch97sbo.jpg","name":"1.jpg","type":"image/jpeg","size":184155,"url":"http://182.92.235.201:8083/wx/storage/fetch/kutxipgk4u754ch97sbo.jpg","addTime":"2022-11-20 14:24:07","updateTime":"2022-11-20 14:24:07"},"errmsg":"成功"}
        String originalFilename = file.getOriginalFilename();
        String contentType = file.getContentType();
        long size = file.getSize();
        //将文件保存D:\tmp\ 并且呢文件名叫banner.png
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i);
        String fileName = originalFilename;
        String uuid = UUID.randomUUID().toString();
        fileName = uuid + "-" + fileName;
        int hashCode = fileName.hashCode();
        String hexString = Integer.toHexString(hashCode) + substring;
        File saveFile = new File(imgPath, hexString);
        file.transferTo(saveFile);
        //http://localhost:8083/picture/img/2f955a30.jpg
        String url = "picture/img/" + hexString;
        MarketStorage marketStorage = objectStorageService.createFile(
                hexString, originalFilename, contentType, size, url);
        BaseRespVo<MarketStorage> baseRespVo = new BaseRespVo<>();
        marketStorage.setUrl(Constant.DOMAIN + marketStorage.getUrl());
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(marketStorage);
        baseRespVo.setErrno(0);
        return baseRespVo;
    }
}
